import 'package:aweb5/util/DialogUtil.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:aweb5/custom/pull_torefresh.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:load/load.dart';
import 'package:aweb5/Constants.dart';
import 'package:aweb5/config/Config.dart';
import 'package:aweb5/util/HttpClient.dart';
import 'package:aweb5/bean/aplus/material_list_entity.dart';
import 'package:aweb5/generated/json/material_list_entity_helper.dart';
import 'package:aweb5/util/toast_util.dart';
import 'package:aweb5/util/NaviUtil.dart';
import 'package:aweb5/pages/fromlib_plan_detail.dart';
import 'package:aweb5/widgetlib/LoadingDialog1.dart';
class MyMaterial extends StatefulWidget {
  String search;
  GlobalKey<MyMaterialState> key;
  MyMaterial(this.search, this.key) : super(key: key);
  @override
  MyMaterialState createState() => MyMaterialState();
}

class MyMaterialState extends State<MyMaterial> {
  RefreshController refreshController =
      RefreshController(initialRefresh: false);
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      requestMaterial();
    });

  }

  BuildContext context;
  @override
  Widget build(BuildContext context) {
    this.context = context;
    return PullToRefresh().buildRefresh(
        refresh: this.refresh,
        load: this.load,
        child: body(),
        pulldown: false,
        pullup: true,
        refreshController: refreshController);
  }

  void refresh() {}


  void setSearchWord(String search){
    widget.search =search;
  }

  void load() {
    currentPage++;
    requestMaterial();
  }

  Widget body() {
    return ListView.builder(
      itemBuilder: (con, ind) {
        return
          GestureDetector(

            child: Container(
                padding: EdgeInsets.symmetric(vertical: ScreenUtil().setHeight(10)),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Image.asset(
                      "assets/assist/wenjian1.png",
                      height: ScreenUtil().setHeight(55),
                      width: ScreenUtil().setWidth(55),
                    ),
                    SizedBox(
                      width: ScreenUtil().setWidth(11),
                    ),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            "${materialListEntity.data.data[ind].name}",
                            style: TextStyle(
                                color: Color(0xff101010),
                                fontSize: ScreenUtil().setSp(14)),
                          ),
                          SizedBox(
                            height: ScreenUtil().setHeight(10),
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                "作者：${materialListEntity.data.data[ind].author}",
                                style: TextStyle(
                                    color: Color(0xffa7a7a7),
                                    fontSize: ScreenUtil().setSp(12)),
                              ),
//                              Text(
//                                materialListEntity.data.data[ind].goodsStatus == 1
//                                    ? "已上架"
//                                    : "",
//                                style: TextStyle(
//                                    color: Color(0xffF44336),
//                                    fontSize: ScreenUtil().setSp(12)),
//                              )
                            ],
                          )
                        ],
                      ),
                    ),
                                                  Text(
                                                    check(materialListEntity.data.data[ind].authStatus)
                                ,
                                style: TextStyle(
                                    color: Color(0xffF44336),
                                    fontSize: ScreenUtil().setSp(12)),
                              ),
                    
                    SizedBox(width: ScreenUtil().setWidth(20),)
                  ],
                )),
            onTap: (){

              if(materialListEntity.data.data[ind].authStatus==1){
//                NaviUtil.push(context, FromLibPlanDetail(null, materialListEntity.data.data[ind].name,sendId: materialListEntity.data.data[ind].id.toString(),edittable: true,),);

                Navigator.of(context).push(new MaterialPageRoute(builder: (c){

                  return FromLibPlanDetail(null, materialListEntity.data.data[ind].name,sendId: materialListEntity.data.data[ind].id.toString(),edittable: true,);
                })).then((value) {
                  requestMaterial();

                });
              }

            },

          )

          ;
      },
      itemCount:
          materialListEntity == null ? 0 : materialListEntity.data.data.length,
    );
  }

  String check(int status){

    if(status==1){
      return "审核通过";
    }
    else if(status==2){
      return "审核不通过";
    }
    else if(status==0){
      return "待审核";
    }else{
      return "";
    }

  }

  int currentPage = 1;
  MaterialListEntity materialListEntity = null;
  void requestMaterial() async {
//    EasyLoading.showSuccess("正在加载……");

//    var v = await showLoadingDialog();

  DialogUtil.showloadingdialog(context);

    try {
      Map<String, dynamic> map = Map<String, dynamic>();

      map["member_id"] = Constants.memberInfoEntity.data.memberId;
      map["type_id"] = "";
      map["keyword"] = widget.search;
      map["page"] = currentPage;

      Map<String, dynamic> map1 = await HttpClient.requestLogin(
          Config.requestApi + 'light/task/index',
          data: map);

      MaterialListEntity loginEntity = new MaterialListEntity();

      loginEntity = materialListEntityFromJson(loginEntity, map1);

       DialogUtil.pop(context);
      if (loginEntity.code == null || loginEntity.code != 200) {
//        Fluttertoast.showToast(msg: "null");
        Toast.show(msg: loginEntity.msg);
//        v.dismiss();
        return;
      }
      if (loginEntity.code == 200) {
//        Toast.show(msg: "创建成功");
//        NaviUtil.pop(this.context);

//        v.dismiss();

//        loginEntity.data.data
//            .retainWhere((element) => element.goodsStatus == 0);

        materialListEntity = loginEntity;
        setState(() {});
      }
    } catch (e) {
      print(e.toString());
      DialogUtil.pop(context);
//      v.dismiss(true);
    }
  }
}
